var auth_state = false;
 
var layer = layui.layer;
 
  var element;
 //注意进度条依赖 element 模块，否则无法进行正常渲染和功能性操作
layui.use('element', function(){
  element = layui.element;
});

	Array.prototype.contain = function(val)
	{
		 for (var i = 0; i < this.length; i++)
		{
		   if (this[i] == val)
		  {
		   return true;
		  }
		}
		 return false;
	};
	
	// 页面初始化 默认执行一次
	checkAuth();
	
	function checkAuth(){
		if(!auth_state){
			checkIp();
		}
	}
	
	
	function setShipName(name,ship){
			switch(ship){
		     case 1:
			 break;
			 case 2:
			 str = "【黄金】";
			 break;
			 case 3:
			 str = "【铂金】";
			 break;
			 case 4:
			 str = "【钻石】";
			 break;
			 case 5:
			 str = "【星耀】";
			 break;
			 case 6:
			 str = "【至尊】";
			 break;
		   }
		   return  str+name;
    }
	
	function getInfo(list,val)
	{
	list = JSON.parse(list);
		 for (var i in list)
		{
		   if (list[i].id == val)
		  {
		   list[i].name = setShipName(list[i].name,list[i].ship);
		   return list[i];
		  }
		}
		 return null;
	};
 
 
  function checkIp(){
	  // 获取IP白名单
  var ips = post("xl/game/ips").data;

  // var arrays2 = ['116.162.3.210','39.144.190.49','119.145.40.27'];

    // 判断IP是否能访问当前页面
	$.get("https://api.ipify.org/?format=json",function(data){
        //parseJSON()函数用于将格式完好的JSON字符串转为与之对应的JavaScript对象
	    // var obj = $.parseJSON(data);
        //转化之后通过obj直接获取json中的属性名称即可,如obj.id输出为1001
        var ip = data.ip;
		if(!ips.contain(ip)){
		 layer.open({
				type: 1,
				offset: 't',
				title: '权限验证失败',
				skin: 'layui-layer-rim', //加上边框
				area: ['270px', '350px'], //设置宽高
				content:'<h5 style="color:red">拒绝访问内测版本!请联系管理员授权！<h5><p>复制ip发给管理员授权:'+ip+'</p>',
				btn: ['复制到剪切板','确定'],
				btn1:function(index, layero){
					copy(ip);
				},btn2:function(index, layero){
					// 按钮2的逻辑
					window.location.href="login.html";
				},
				cancel: function(){
					// 右上角关闭事件的逻辑
					window.location.href="login.html";
				}
		});
			layer.open({
			offset: 't',
			content: "<div>拒绝访问内测版本!请联系管理员授权！</div>",
			btn: ['关闭'],
			btn1:function(index, layero){
			 layer.close(index);
			}
			})
		}else{
			auth_state = true;
		}
   }); 
  }

   
   function copy (text) {
	  // text是复制文本
	  // 创建input元素
	  const el = document.createElement('input')
	  // 给input元素赋值需要复制的文本
	  el.setAttribute('value', text)
	  // 将input元素插入页面
	  document.body.appendChild(el)
	  // 选中input元素的文本
	  el.select()
	  // 复制内容到剪贴板
	  document.execCommand('copy')
	  // 删除input元素
	  document.body.removeChild(el)
	  layer.alert("已复制到剪切板",{icon:0,offset: 't'});
	}

   // 初始化境界
   //var jj = window.localStorage.getItem("jj");
   //  if(jj==null){
//	window.localStorage.setItem("jj","凡人一重");
	// jj = "凡人一重";
 // }
  
  // document.getElementById("jj").innerHTML = jj;
  
  var jy = window.localStorage.getItem("jy");
  if(jy==null){
   window.localStorage.setItem("jy",0);
  }
 
  // document.getElementById("jy").innerHTML = jy;
  //初始化数量
   var zmlnum = window.localStorage.getItem("zml");
  if(zmlnum==null || zmlnum == 'NaN'){
	window.localStorage.setItem("zml",0);
	 zmlnum = 0
  }
  document.getElementById("zml").innerHTML = zmlnum;
  
  var zmlcodenum = window.localStorage.getItem("zmlcode");
  if(zmlcodenum==null){
   window.localStorage.setItem("zmlcode",0);
	 zmlcodenum = 0
  }
  document.getElementById("zmlcode").innerHTML = zmlcodenum;
  
 
  function add(){
	checkAuth();
  // 获取当前经验
   var jy = window.localStorage.getItem("jy");
   var newJy = Number(jy)+1000;
   window.localStorage.setItem("jy",newJy);
    layer.alert("新增经验1000点",{icon:0,offset: 't'});
   document.getElementById("jy").innerHTML=newJy;
   element.progress('test', '10%');
 }
 
 function tp(){
       layer.alert("经验不足",{icon:0,offset: 't'});
 }

function qcl(){
	checkAuth();
}

function notice(){
	checkAuth();
    var loading = layer.load(2, {
        shade: [0.8, '#fff'],
        content:'正在加载，请耐心等待......',
        success: function (layerContentStyle) { // 设置loading样式
            layerContentStyle.find('.layui-layer-content').css({
                'padding-left': '45px',
                'text-align': 'left',
                'width': '175px',
                'line-height':'30px',
				'margin-top':'-370px'
            });
        }
		});

	  // 判断图鉴列表是否有缓存数据
	  //var noticeList = window.localStorage.getItem("noticeList");
	 // var result = [];
	  //if(noticeList!=null && noticeList!=""){
	//	result = noticeList;
	 // }else{
	   // 请求接口 获取图鉴列表
	   result = post("xl/game-figure/list").data;
	  //}
     
	  if(result.length>0){
	  var str = "";
	  var contents = '<br/><br/><div style="padding:5px;">';
	  //console.info(result);
	  window.localStorage.setItem("noticeList",JSON.stringify(result));
	  for(var i in result){
	  //console.info(result[i].id);
	    if(result[i].name!=null){
			result[i].name = setShipName(result[i].name,result[i].ship);
			contents += '<button  onclick="info('+result[i].id+')" type="button" class="layui-btn layui-btn-warm layui-btn-ms">'+result[i].name+'</button>';
		}
	  }
	  contents += "</div>"; 
		 
	 layer.close(loading);
	  //console.info(contents); 
		 
      //示范一个公告层
      layer.open({
		offset: 't',
        type: 1
        ,title: '图鉴' //不显示标题栏
        ,closeBtn: false
        ,area: ['390px', '70%']
        ,shade: 0.4
        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
        ,btn: ['关闭']
        ,btnAlign: 'c'
        ,moveType: 1 //拖拽模式，0或者1
        ,content:contents
        ,success: function(layero){
          var btn = layero.find('.layui-layer-btn');
		  var r = layero.find('#layui-layer1');
			r.attr({"style":"top:131px"})
          btn.find('.layui-layer-btn0').attr({
          });
        }
      });
	  }
}


function info(val){

	var noticeList = window.localStorage.getItem("noticeList");
	var data = getInfo(noticeList,val);
	var str = "主角";
	var ms = "是一个传奇人物,祖境强者,极为擅长法术,曾以一己之力覆灭整个东灵域";
	var ptjn = "咚咚拳";
	var zsjn = "呵斥一声";
	var ptjndesc = "攻击单个敌人,造成100%法术伤害";
	var zsjndesc = "造成10次160%的技能伤害,随机使敌方必中眩晕3回合、冰冻3回合、削弱50%防御、混乱3回合";
	
	if(data!=null){
		str = data.name;
		ms = data.detail;
		ptjn = data.generalSkill;
		ptjndesc = data.exclusiveSkill;
		zsjn = data.generalSkillDesc;
		zsjndesc = data.exclusiveSkillDesc;
	}
    //示范一个公告层
      layer.open({
		offset: 't',
        type: 1
        ,title: '图鉴' //不显示标题栏
        ,closeBtn: false
        ,area: ['390px', '560px']
        ,shade: 0.4
        ,id: 'LAY_layuidm' //设定一个id，防止重复弹出
        ,btn: ['关闭']
        ,btnAlign: 'c'
        ,moveType: 0 //拖拽模式，0或者1
        ,content: '<center style="background-color:#e5d6ad"><br/><br/><h4 style="color:red">'+str+'【凡人1品】【+0】【0级】<h4><br/><p style="color:black">'+ms+'</p>'+
		'<br/><br/><hr/><center style="color:red">------总属性------</center><p style="color:black">生命:10380&nbsp;&nbsp;攻击:2100&nbsp;&nbsp; 物防:780</p><br/><p style="color:black">法防:700&nbsp;&nbsp; 命中:100%&nbsp;&nbsp; 暴击:7.5%</p><br/><p style="color:black">闪避:3.5% &nbsp;&nbsp;抗暴:3.5%</p><br/><hr/><center style="color:red">------技能说明------</center><br/><p>【普通技能】'+ptjn+'</p><br/>'
		+'<p style="color:black">'+ptjndesc+'</p><br/><p>【专属技能】'+zsjn+'</p><br/><p style="color:black">'+zsjndesc+'</p><br/><br/></center>',success: function(layero){
          var btn = layero.find('.layui-layer-btn');
          btn.find('.layui-layer-btn0').attr({
          });
        }
      });
}
 
 
function zd(){
	checkAuth();
    result = post("xl/game/users").data;
	debugger
	if(result==undefined){
		layer.alert("战斗失败-服务异常",{icon:0,offset: 't'});
	}
	
     var contents =  '<br/><br/><div style="padding:15px;"><h2>请选择操作的用户↓</h2>';
	  if(result.length>0){
		  for(var i in result){
			if(result[i].jobName!=null){
				contents += '<button  onclick="userInfo('+result[i].userId+')" type="button" class="layui-btn layui-btn-normal layui-btn-ms">'+result[i].jobName+'</button>';
			}
		  }
	  }
	  contents += "</div>"; 
	  
    //示范一个公告层
      layer.open({
		offset: 't',
        type: 1
        ,title: '战斗选择' //不显示标题栏
        ,closeBtn: false
        ,area: ['390px', '560px']
        ,shade: 0.4
        ,id: 'LAY_layuidm_p' //设定一个id，防止重复弹出
        ,btn: ['关闭']
        ,btnAlign: 'c'
        ,moveType: 0 //拖拽模式，0或者1
        ,content: contents
		,success: function(layero){
          var btn = layero.find('.layui-layer-btn');
          btn.find('.layui-layer-btn0').attr({
          });
        }
      });
}


function userInfo(uid){

 result = post("xl/game/copys").data;
     var contents =  '<br/><br/><div style="padding:15px;"><h2>请选择挑战的Boss↓</h2>';
	  if(result.length>0){
		  for(var i in result){
			if(result[i].jobName!=null){
				contents += '<button  onclick="zdForBoss('+uid+','+result[i].copyId+')" type="button" class="layui-btn layui-btn-danger layui-btn-ms">'+result[i].jobName+'</button>';
			}
		  }
	  }
	  contents += "</div>"; 
	  
    //示范一个公告层
      layer.open({
		offset: 't',
        type: 1
        ,title: 'BOSS列表' //不显示标题栏
        ,closeBtn: false
        ,area: ['390px', '560px']
        ,shade: 0.4
        ,id: 'LAY_layuidm_t' //设定一个id，防止重复弹出
        ,btn: ['关闭']
        ,btnAlign: 'c'
        ,moveType: 0 //拖拽模式，0或者1
        ,content: contents
		,success: function(layero){
          var btn = layero.find('.layui-layer-btn');
          btn.find('.layui-layer-btn0').attr({
          });
        }
      });

}

function zdForBoss(uid,cid){
 var params = {"userId":uid,"copyId":cid,"type":"2"};
 result = postForParams("xl/game/pk",params).data.infos;
     var contents =  '<br/><br/><div style="padding:15px;"><h2>战斗结果</h2>';
	  if(result.length>0){
		  for(var i in result){
			if(result[i].currentRound!=null){
				contents += '<h4 style="color:red;font-size:18;">'+result[i].currentRound+'</h4>'+
				'<span style="color:#ff00bf;">'+result[i].record1+'</span><br/>'+
				'<span style="color:#ff00bf;">'+result[i].record2+'</span><br/>'+
				'<span style="color:#ff00bf;">'+result[i].record3+'</span><br/>'+
				'<span style="color:#ff00bf;">'+result[i].record4+'</span><br/>';
				
			  var map = result[i];
			  for (var key in map) {
			     if(key!="record1"&&key!="record2"&&key!="record3"&&key!="record4"&&key!="currentRound"){
				 contents +='<span>'+map[key]+'</span><br/>';
				 }
			  }
			}
		  }
	  }
	  contents += '<h2 style="color:red;">------战斗结束------</h2><hr/><br/></div>'; 
	  
    //示范一个公告层
      layer.open({
		offset: 't',
        type: 1
        ,title: 'BOSS列表' //不显示标题栏
        ,closeBtn: false
        ,area: ['390px', '560px']
        ,shade: 0.4
        ,id: 'LAY_layuidm_o' //设定一个id，防止重复弹出
        ,btn: ['关闭']
        ,btnAlign: 'c'
        ,moveType: 0 //拖拽模式，0或者1
        ,content: contents
		,success: function(layero){
          var btn = layero.find('.layui-layer-btn');
          btn.find('.layui-layer-btn0').attr({
          });
        }
      });
}


  function cq(){
	checkAuth();
	restartNum();
   if(zmlnum<=0){
	  layer.alert("招募令不足",{icon:0,offset: 't'});
	  return false;
   }

   var arrays = tjs().arrays;
   var results = tjs().results;
   var size = arrays.length;
   var data = Number(Math.round(Math.random()*(size-1-0)+0));
   var id = 0;
   
   
   for(var i in results){
		if(results[i].name==arrays[data]){
			debugger
			id = results[i].id;
		}
   }
   
   var str = '<span style="color:red;border-style:solid;border-color:#ff0000;width:145px;margin-top:5px;margin-left:5px;" onclick="info('+id+')">'+arrays[data]+'</span>';
  
   debugger
    $("#dy").append(str);
	
   	layer.open({
		title: '招募',
		offset: 't',
		content:str,
		btn: ['关闭'],
		btn1:function(index, layero){
		 layer.close(index);
		}
	});
	
   
   var jc = document.getElementById("jc").value;
   //document.getElementById("jc").value = jc+">>>>>>\n"+arrays[data];
   window.localStorage.setItem("zml",Number(zmlnum)-1);
   window.localStorage.setItem("zmlcode",Number(zmlcodenum)+10);
   restartNum();
   restartZml();
   return;
 }
 
 function slc(){
   restartNum();
   if(zmlnum<10){
	  layer.alert("招募令不足",{icon:0,offset: 't'});
	  return false;
   }
   
   var arrays = tjs().arrays;
   var size = arrays.length;
   var data = "";
   var str = "";
   var jc = document.getElementById("jc").value;
   for(var i=0;i<10 ;i++){
       data = Number(Math.round(Math.random()*(size-1-0)+0));
	   str+= '<span style="color:red;border-style:solid;border-color:#ff0000;width:145px;height:50px;margin-top:10px;margin-left:7px;display: inline">'+arrays[data]+'</span>'
	   
   }
   
  $("#dy").append(str);
   
   // document.getElementById("jc").value = jc+">>>>>>\n"+str;
	layer.open({
		title: '十连抽',
		offset: 't',
		content: '<div>'+str+'</div>',
		btn: ['关闭'],
		btn1:function(index, layero){
		 layer.close(index);
		}
	});
   
   window.localStorage.setItem("zml",Number(zmlnum)-10);
   window.localStorage.setItem("zmlcode",Number(zmlcodenum)+100);
   restartNum();
   restartZml();
   return;
 }


function queryInfo(){
	layer.alert("神秘人物无法查看",{icon:0,offset: 't'});
}

function clearCatch(){
window.localStorage.removeItem("token");	
window.localStorage.removeItem("jwt");
initToken();
layer.alert("成功清理缓存",{icon:0,offset: 't'});
}

 
 function addcq(){
checkAuth();
 window.localStorage.setItem("zml",Number(zmlnum)+1);
 restartNum();
 restartZml();
 }
 
  function addzml(){
	checkAuth();
 window.localStorage.setItem("zml",Number(zmlnum)+100);
 restartNum();
 restartZml();
 }
 
 
 function tjs(){
	
	 var arrays = [];
	  result = post("xl/game-figure/list").data;
	  for(var i in result){
	  //console.info(result[i].id);
	    if(result[i].name!=null){
			result[i].name = setShipName(result[i].name,result[i].ship);
			arrays.push(result[i].name);
			//contents += '<button  onclick="info('+result[i].id+')" type="button" class="layui-btn layui-btn-warm layui-btn-ms">'+result[i].name+'</button>';
		}
	  }
	  window.localStorage.setItem("noticeList",JSON.stringify(result));
	  var datas = {"arrays":arrays,"results":result};
	  return datas;
 }
 
 function updateDY(str){
	 var dy = window.localStorage.getItem("dy");
	 window.localStorage.setItem("dy",dy+str);
 }
 
  function clearDY(){
	 window.localStorage.removeItem("dy");
	 window.localStorage.setItem("dy","");
	 layer.alert("清空完成",{icon:0,offset: 't'});	
 }
 
  function dh(){
	layer.alert("兑换暂未开发",{icon:0,offset: 't'});	
 }
 
 function restartNum(){
 zmlnum = window.localStorage.getItem("zml");
  zmlcodenum = window.localStorage.getItem("zmlcode");
 }
 
  function restartZml(){
	document.getElementById("zml").innerHTML = zmlnum;
  document.getElementById("zmlcode").innerHTML = zmlcodenum;
 }